<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/22
 * Time: 15:49
 */
namespace app\index\controller;
use app\index\model\Car;
use app\index\model\Goods;
use app\index\model\Manager;
use think\App;
use think\Request;
use think\facade\Session;
class Jxcar extends Common
{
    public function __construct(App $app = null, Request $request)
    {
        parent::__construct($app, $request);
    }


    //查询购物车列表
    public function index(){
        $sid=Session::get("shop_id");
        $where=array('sid' => $sid,'status' => 0);
        $data['sum']=Car::getsum($where);
        $data['data']=Car::getlist($where);
        $this->return = $data;
    }
    //增加购物车数量
    public function increase(){
        $post=$this->param;
        $where=array('id' => $post['car_id']);
        $res=Car::getone($where);
        if ($res) {
            $num=$res['num']+1;
            $sum=$res['sum']+$res['price'];
            $u_data=array('num' => $num,'sum' => $sum);
            $re=Car::updatecar($u_data,$where);
            if ($re) {
                $data['code']='1001';
                $data['msg']='';
            }else{
                $data['code']='1002';
                $data['msg']='减少失败';
            }
        }else{
            $data['code']='1002';
            $data['msg']='参数错误';
        }
        $this->return = $data;
    }
    //减少购物车数量
    public function reduce(){
        $post=$this->param;
        $where=array('id' => $post['car_id']);
        $res=Car::getone($where);
        if ($res) {
            if ($res['num']>1) {
                $num=$res['num']-1;
                $sum=$res['sum']-$res['price'];
                $u_data=array('num' => $num,'sum' => $sum);
                $re=Car::updatecar($u_data,$where);
                if ($re) {
                    $data['code']='1001';
                    $data['msg']='';
                }else{
                    $data['code']='1002';
                    $data['msg']='减少失败';
                }
            }else{
                $data['code']='1002';
                $data['msg']='数量已达最小';
            }
        }else{
            $data['code']='1002';
            $data['msg']='参数错误';
        }
        $this->return = $data;
    }
    //清空购物车
    public function emptycar(){
        $where=array('sid' => Session::get("shop_id"),'status' => 0);
        $res=Car::emptycar($where);
        if ($res) {
            $data['code']='1001';
            $data['msg']='';
        }else{
            $data['code']='1002';
            $data['msg']='参数错误';
        }
        $this->return = $data;
    }
    //加入购物车
    public function add(){
        $post=$this->param;
        $data['code']='1002';
        $data['msg']='';
        if(empty($post['list'])){
            $data['code']='1002';
            $data['msg']='同步内容为空';
        }else{
            foreach ($post['list'] as $key => $value) {
                $res=Car::getone(array('id' => $value['car_id']));
                if ($res) {
                    $updates['title']=$value['sn'];
                    $updates['sid']=$value['sid'];
                    $updates['gid']=$value['gid'];
                    $updates['integral']=$value['integral'];
                    $updates['uid']=$value['uid'];
                    $updates['username']=$value['username'];
                    $updates['num']=$value['num'];
                    $updates['money']=$value['money'];
                    $updates['paytype']=$value['paytype'];
                    $updates['cancetime']=$value['cancetime'];
                    $updates['remark']=$value['remark'];
                    $updates['status']=$value['status'];
                    $updates['timeline']=$value['timeline'];
                    $updates['moblie']=$value['moblie'];
                    $re=Car::updateorder($updates,array('id' => $value['car_id']));
                }else{
                    $add_data['id']=$value['car_id'];
                    $add_data['title']=$value['sn'];
                    $add_data['sid']=$value['sid'];
                    $add_data['gid']=$value['gid'];
                    $add_data['integral']=$value['integral'];
                    $add_data['uid']=$value['uid'];
                    $add_data['username']=$value['username'];
                    $add_data['num']=$value['num'];
                    $add_data['money']=$value['money'];
                    $add_data['paytype']=$value['paytype'];
                    $add_data['cancetime']=$value['cancetime'];
                    $add_data['remark']=$value['remark'];
                    $add_data['status']=$value['status'];
                    $add_data['timeline']=$value['timeline'];
                    $re=Car::add($add_data);
                }
                if ($re) {
                    $data['code']='1001';
                }
            }
        }
        $this->return = $data;
    }
}

